import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 28779
 * Date: 2024-03-29
 * Time: 20:36
 */

//用哈希表的方式
class Solution1 {
    public int majorityElement(int[] nums) {
        int n = nums.length/2;
        Map<Integer, Integer> counts = countNums(nums);
        //Map.Entry<Integer, Integer> majorityentry = null;
        for (Map.Entry<Integer, Integer> entry : counts.entrySet()) {
            if (entry.getValue() > n) {
                return entry.getKey();
            }
        }
        return 0;
    }

    private Map<Integer, Integer> countNums(int[] nums) {
        Map<Integer, Integer> count = new HashMap<>();
        for (int num : nums) {
            if(!count.containsKey(num)) {
                count.put(num, 1);
            } else {
                count.put(num, count.get(num) + 1);
            }
        }
        return count;
    }
}
public class Method2 {
    public static void main(String[] args) {
        int[] nums = {2,2,1,1,1,2,2};
        Solution1 solution1 = new Solution1();
        int n = solution1.majorityElement(nums);
        System.out.println(n);
    }
}